package com.ruoyi.web.controller.system;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EntityCustomer;
import com.ruoyi.system.service.IEntityCustomerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户Controller
 * 
 * @author ruoyi
 * @date 2020-12-27
 */
@Controller
@RequestMapping("/system/customer")
public class EntityCustomerController extends BaseController
{
    private String prefix = "system/customer";

    @Autowired
    private IEntityCustomerService entityCustomerService;

    @RequiresPermissions("system:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询客户列表
     */
    @RequiresPermissions("system:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EntityCustomer entityCustomer)
    {
        startPage();
        List<EntityCustomer> list = entityCustomerService.selectEntityCustomerList(entityCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @RequiresPermissions("system:customer:export")
    @Log(title = "导出客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EntityCustomer entityCustomer)
    {
        List<EntityCustomer> list = entityCustomerService.selectEntityCustomerList(entityCustomer);
        ExcelUtil<EntityCustomer> util = new ExcelUtil<EntityCustomer>(EntityCustomer.class);
        return util.exportExcel(list, "customer");
    }

    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @RequiresPermissions("system:customer:add")
    @Log(title = "新增客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(EntityCustomer entityCustomer)
    {
        return toAjax(entityCustomerService.insertEntityCustomer(entityCustomer));
    }

    /**
     * 修改客户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EntityCustomer entityCustomer = entityCustomerService.selectEntityCustomerById(id);
        mmap.put("entityCustomer", entityCustomer);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户
     */
    @RequiresPermissions("system:customer:edit")
    @Log(title = "修改客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(EntityCustomer entityCustomer)
    {
        return toAjax(entityCustomerService.updateEntityCustomer(entityCustomer));
    }

    /**
     * 删除客户
     */
    @RequiresPermissions("system:customer:remove")
    @Log(title = "删除客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult remove(String ids)
    {
        return toAjax(entityCustomerService.deleteEntityCustomerByIds(ids));
    }

    /**
     * 检查是否已经有该手机号
     */
    @PostMapping("/checkPhone")
    @ResponseBody
    @RepeatSubmit
    public boolean checkPhone(EntityCustomer entityCustomer){
        String phone = entityCustomer.getPhone();
        if(StringUtils.isEmpty(phone)){
            return false;
        }
        boolean existPhone = entityCustomerService.isExistPhone(phone,entityCustomer.getId());
        return !existPhone;
    }
    /**
     * 检查是否已经有该身份证号
     */
    @PostMapping("/checkIdCard")
    @ResponseBody
    @RepeatSubmit
    public boolean checkIdCard(EntityCustomer entityCustomer){
        String idCard = entityCustomer.getIdCard();
        if(StringUtils.isEmpty(idCard)){
            return false;
        }
        boolean existIdCard = entityCustomerService.isExistIdCard(idCard,entityCustomer.getId());
        return !existIdCard;
    }
    @GetMapping("/queryValidCustomerByParam")
    @ResponseBody
    public AjaxResult queryValidCustomerByParam(String param){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);

//       List<EntityCustomer> result = Collections.emptyList();
//        if(StringUtils.isEmpty(param)){
//            ajax.put("value", result);
//        }else{
//
//        }
        ajax.put("value", entityCustomerService.queryValidCustomerByParam(param));
        return ajax;
    }

    @GetMapping("/getTotalCustomerCount")
    @ResponseBody
    public AjaxResult getTotalCustomerCount(){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", entityCustomerService.getTotalCustomerCount());
        return ajax;
    }
    @GetMapping("/queryLastYearEveryMothTotalCustomerCount")
    @ResponseBody
    public AjaxResult queryLastYearEveryMothTotalCustomerCount(){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", entityCustomerService.queryLastYearEveryMothTotalCustomerCount());
        return ajax;
    }
}
